﻿<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
    <title>综合报表</title>
    <style>
        .pagecontent {
            width: 100%;
            height: 100%;
            padding: 20px;
            box-sizing: border-box;
        }

        .header{
            position:relative;
            margin:auto;
            width:80%;
            line-height: 58px;
        }

        .pagecontent .toolarea {
            display: block;
            line-height: 40px;
        }

        .toolarea .spanstate {
            font-size: 14px;
            cursor: pointer;
            padding-left: 10px;
            padding-right: 10px;
        }

        .toolarea input[type=button] {
            display: inline-block;
            padding: 4px;
            border: 0;
            background-color: rgb(63, 218, 89);
            border-radius: 2px;
            cursor: pointer;
            padding-left: 20px;
            padding-right: 20px;
            color: white;
        }

        .toolarea .selected {
            font-weight: bold;
        }

        #chartarea {
            width: 100%;
            height: 500px;
            position: relative;
            margin: auto;
        }

        ul {
            display: flex;
            flex-direction: row;
        }

        li {
            flex: 1;
            text-align: center;
            cursor: pointer;
        }

        .bodyer {
            width: 100%;
            height: 500px;
            position: relative;
        }

        .chartcontext {
            position: relative;
            height: 100%;
            width: 100%;
        }

        .apper {
            position: relative;
            width:80%;
            margin:auto;
        }

        .apper ul {
            position: relative;
            display: flex;
            flex-direction: row;
            text-align: center;
            padding-left: 10px;
        }

        .apper li {
            cursor: pointer;
            width: 300px;
            margin-right: 10px;
        }

        .apper .selected .item {
            border: none;
            border-left:2px solid #fc8231;
            padding-left:8px;
        }

        .apper li .item {
            position: relative;
            width: 100%;
            text-align: left;
            font-weight: 100;
            display: flex;
            flex-direction: column;
            border: 1px dashed #888;
            padding: 10px;
        }

        .pagereport {
            padding: 20px;
        }

        .areadate {
            position: fixed;
            left: 0;
            top: 20px;
            width: 80px;
            z-index: 100;
        }

        .areadate ul {
            display: flex;
            flex-direction: column;
        }

        .areadate li {
            display: block;
            font-size: 14px;
        }

        .areadate .selected {
            color: red;
        }
    </style>
</head>

<body>
    <div class="pagereport">

        <div class="areadate">
            <ul>
                <li class="selected" dataid="hour">最近</li>
                <li dataid="today">今日</li>
            </ul>
        </div>

        <div class="arealinux">
            <div class="header">
                <ul>
                </ul>
            </div>
            <div class="bodyer">
                <div class="chartcontext" id="chartlinux"></div>
            </div>
        </div>

        <div class="areaservice">
            <div class="header">
                <ul>
                </ul>
            </div>
            <script type="text/html" id="templateapp">
                <li dataid="[id]" class="[select]">
                    <div class="item">
                        <div class="wlin">[service:name]([service:code])</div>
                        <div class="wlin">[appid]</div>
                        <div class="wlin">[name]</div>
                    </div>
                </li>
            </script>
            <div class="apper">
                <ul>

                </ul>
            </div>
            <div class="bodyer">
                <div class="chartcontext" id="chartapp"></div>
            </div>
        </div>



    </div>


    <!--_footer 作为公共模版分离出去-->
    <script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
    <script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
    <!-- <script type="text/javascript" src="../lib/My97DatePicker/4.8/WdatePicker.js"></script> -->
    <script type="text/javascript" src="../lib/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
    <script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script>
    <!--/_footer 作为公共模版分离出去-->
    <!--请在下方写此页面业务相关的脚本-->
    <!-- <script type="text/javascript" src="../lib/My97DatePicker/4.8/WdatePicker.js"></script> -->


    <script type="text/javascript" src="../lib/api.js"></script>
    <!-- <link href="../lib/page.min.css" rel="stylesheet" />
    <script src="../lib/page.min.js"></script> -->

    <script type="text/javascript" src="echarts.min.js"></script>

    <script>

        // var pageobject = null;

        var issecond = true;
        var linuxid = 1000;

        var _datetype = "hour";

        var _selectedlinuxid = 0;
        var _selectedappid = 0;

        function _showpagedata(datas, chartid) {

            // var item = datas[k];
            //     var htmlStr = '<tr class="text-c">';
            //     htmlStr += '<td>' + item.id + '</td>';
            //     htmlStr += '<td>' + item.linuxPointId + '</td>';
            //     htmlStr += '<td>' + item.dataDate + '</td>';
            //     htmlStr += '<td>' + item.cpuUsed + '%</td>';
            //     htmlStr += '<td>' + item.cpU1 + '% --- '+item.cpU2+'% --- '+item.cpU3+'%</td>';
            //     htmlStr += '<td>' + item.memoryUsed + 'MB/'+item.memoryTotal+'MB</td>';
            //     htmlStr += '</tr>';
            //     hstr += htmlStr;


            var option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                toolbox: {
                    show: true,
                    feature: {
                        magicType: { show: true, type: ['stack', 'tiled'] },
                        saveAsImage: { show: true }
                    }
                },
                legend: {
                    data: ['CPU', '内存占用', '流量入', '流量出']
                },
                xAxis: {
                    data: []
                },
                yAxis: [
                    {
                        type: 'value',
                        name: 'CPU',
                        min: 0,
                        max: 100,
                        position: 'left',
                        offset: 30,
                        axisLabel: {
                            formatter: '{value}%'
                        }
                    },
                    {
                        type: 'value',
                        name: '内存占用',
                        position: 'left',
                        nameLocation: 'end',
                        offset: -30,
                        axisLabel: {
                            formatter: '{value}MB'
                        }
                    },
                    {
                        type: 'value',
                        name: '流量入',
                        position: 'right',
                        nameLocation: 'end',
                        offset: -40,
                        axisLabel: {
                            formatter: '{value}MB'
                        }
                    },
                    {
                        type: 'value',
                        name: '流量出',
                        position: 'right',
                        nameLocation: 'end',
                        offset: 40,
                        axisLabel: {
                            formatter: '{value}MB'
                        }
                    }
                ],
                series: []
            };

            var cpuarray = new Array();
            var memoryarray = new Array();

            var netinarray = new Array();
            var netoutarray = new Array();

            for (var k = 0; k < datas.length; k++) {
                var item = datas[k];
                option.xAxis.data.push(item.dataDate);
                cpuarray.push(item.cpuUsed);
                memoryarray.push(item.memoryUsed);
                netinarray.push(item.netIn);
                netoutarray.push(item.netOut);
            }

            // var maxval = Math.max(...cpuarray);
            // if (maxval < 50) {
            //     option.yAxis[0].max = parseInt(maxval * 3 + 1);
            //     console.log(option.yAxis[0].max);
            // } else {
            //     option.yAxis[0].max = 100;
            // }

            var maxval = Math.max(...cpuarray);
            if (maxval < 50) {
                option.yAxis[0].max = Number((maxval * 3)).toFixed(2);
                console.log(option.yAxis[0].max);
            } else {
                option.yAxis[0].max = 100;
            }


            option.series.push({ smooth: true, name: 'CPU', type: 'line', data: cpuarray, yAxisIndex: 0 });

            option.series.push({ smooth: true, name: '内存占用', type: 'line', data: memoryarray, yAxisIndex: 1 });

            option.series.push({ smooth: true, name: '流量入', type: 'line', data: netinarray, yAxisIndex: 2 });

            option.series.push({ smooth: true, name: '流量出', type: 'line', data: netoutarray, yAxisIndex: 3 });

            //初始化echarts实例
            var myChart = echarts.init(document.getElementById(chartid));

            //使用制定的配置项和数据显示图表
            myChart.setOption(option);
        }


        $(window).ready(function () {


            _apiget("/api/spider/User/readMyInfo", false, function (cuser, cobj) {
                if (cuser == 200) {
                    if (cobj.userName != undefined) {
                        $(".username").html(cobj.userName);
                        logined = true;

                        window.parent.SetNickName(cobj.userName);
                    }


                    _apiget('/api/spider/linuxInfo', false, function (code, obj) {
                        if (code == 200 && obj.items.length>0) {
                            var ahtml = '';
                            var hitlinuxid = 0;
                            for (var k = 0; k < obj.items.length; k++) {
                                var item = obj.items[k];
                                if (k == 0) {
                                    ahtml += '<li dataid="' + item.id + '" class="selected">' + item.name + '</li>';
                                    hitlinuxid = item.id;
                                } else {
                                    ahtml += '<li dataid="' + item.id + '">' + item.name + '</li>';
                                }
                            }
                            $(".arealinux").find(".header").find("ul").html(ahtml);
                            readlinuxcharts(hitlinuxid);
                        }
                    })


                    //读取项目列表
                    _apiget('/api/spider/projectInfo/readProject', false, function (code, obj) {
                        if (code == 200 && obj.length>0) {
                            var ahtml = '';
                            var hitprojectid = 0;
                            for (var k = 0; k < obj.length; k++) {
                                var item = obj[k];
                                if (k == 0) {
                                    ahtml += '<li dataid="' + item.id + '" class="selected">' + item.name + '</li>';
                                    hitprojectid = item.id;
                                } else {
                                    ahtml += '<li dataid="' + item.id + '">' + item.name + '</li>';
                                }
                            }
                            $(".areaservice").find(".header").find("ul").html(ahtml);
                            //根据项目ID读取app列表
                            readappbyprojectid(hitprojectid);
                        }
                    })


                }
            });

            //读取服务器列表


            $(".arealinux").on('click', 'li', function () {
                $(this).addClass('selected');
                $(this).siblings().removeClass('selected');

                var appid = $(this).attr('dataid');
                var intprojectid = parseInt(appid);
                readlinuxcharts(intprojectid);

            })

            $(".areaservice").on('click', 'li', function () {
                $(this).addClass('selected');
                $(this).siblings().removeClass('selected');
                if ($(this).parent().parent().hasClass("apper")) {
                    var appid = $(this).attr('dataid');
                    var intappid = parseInt(appid);
                    readappcharts(intappid);
                } else {
                    var appid = $(this).attr('dataid');
                    var intprojectid = parseInt(appid);
                    readappbyprojectid(intprojectid);
                }
            })

            $(".areadate").on('click', 'li', function () {
                _datetype = $(this).attr('dataid');
                $(this).addClass('selected');
                $(this).siblings().removeClass('selected');

                $(".arealinux").find("li").eq(0).trigger('click');

                $(".apper").find("li").eq(0).trigger('click');
            })
        });


        /**
         * 读取服务器的报表数据
         */
        function readlinuxcharts(_linuxid) {
            if(_linuxid==0){
                return;
            }
            _selectedlinuxid = _linuxid;
            var info = {
                linuxid: _linuxid,
                datetype: _datetype
            };
            _apipost('/api/spider/user/ReadCharts', true, JSON.stringify(info), function (code, obj) {
                if (code == 200) {
                    _showpagedata(obj, "chartlinux");
                }
            })
        }

        /**
         * 读取容器的报表数据
         */
        function readappcharts(_appid) {
            _selectedappid = _appid
            var info = {
                linuxid: 0,
                datetype: _datetype,
                appid: _appid
            };
            _apipost('/api/spider/user/ReadCharts', true, JSON.stringify(info), function (code, obj) {
                if (code == 200) {
                    _showpagedata(obj, "chartapp");
                }
            })
        }


        /**
         * 根据项目读取容器信息
         */
        function readappbyprojectid(_projectid) {
            _apiget('/api/spider/appInfo/searchApp?projectid=' + _projectid, true, function (code, obj) {
                if (code == 200) {
                    if(typeof obj =="string"){
                        obj =JSON.parse(obj);
                    }
                    if (obj.length > 0) {
                        var templatehtml = $("#templateapp").html();
                        var hitappid = 0;
                        var ahtml = '';
                        for (var k = 0; k < obj.length; k++) {
                            var item = obj[k];
                            if (k == 0) {
                                item.select = 'selected';
                                hitappid = item.id;
                            } else {
                                item.select = '';
                            }
                            ahtml += _buildtemplate(templatehtml, item);
                        }
                        $(".areaservice").find(".apper").find("ul").html(ahtml);
                        if (hitappid > 0) {
                            readappcharts(hitappid);
                        }
                    }

                }
            });
        }


    </script>


</body>

</html>